package com.nativetalk.service.four.index;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.banner.TdNtBannerInfoIndex;
import com.nativetalk.bean.category.TsNtCategoryIndex;
import com.nativetalk.bean.category.TsNtCategoryIndexNew;
import com.nativetalk.bean.dict.TsNtDictDeatil;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.setmeal.TdNtSetMealIsTrial;
import com.nativetalk.bean.teacher.TdNtTeacherList;
import com.nativetalk.common.Lang;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.*;
import com.nativetalk.dto.TeacherRecommendResponse;
import com.nativetalk.dto.UpcomingResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2016/10/27.
 */
@Service
public class FourIndexServiceImpl implements FourIndexService{
    private static Logger logger = Logger.getLogger(FourIndexServiceImpl.class);
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtTeacherLabelDao tdNtTeacherLabelDao;
    @Resource
    private TdNtReserveDao tdNtReserveDao;
    @Resource
    private TdNtMembSetmealDao tdNtMembSetmealDao;

    @Override
    public RetInfo index(Integer memb_id) {
        String logInfo = this.getClass().getName() + ":index:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            /**
             * banner列表(图片,链接,标题)
             */
            Map<String , Object> map = new HashMap<>();
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap = (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);

            map.put("waijiaojunIntroVideoUrl", "http://file.waijiaojun.com/teenager_waijiaojun/introduction/waijiaojun_teenager_introduction.mp4");
            List<TdNtBannerInfoIndex> tdNtBannerInfoIndices = (List<TdNtBannerInfoIndex>) MemcachedUtils.get(MemcachedKey.bannerInfoAPPIndexs);
            map.put("banner",tdNtBannerInfoIndices);
            System.out.println("fdafafa");
            /**
             * 课程类别(id,图标,名称)
             */
            List<TsNtCategoryIndex> tsNtCategoryIndices =  (List<TsNtCategoryIndex>)MemcachedUtils.get(MemcachedKey.tsNtCategoryIndices);
            List<TsNtCategoryIndexNew> tsNtCategoryIndexNews = new ArrayList<>();
            for (int i = 0 ; i < tsNtCategoryIndices.size() ; i ++ ){
                TsNtCategoryIndexNew tsNtCategoryIndexNew =  new TsNtCategoryIndexNew();
                tsNtCategoryIndexNew.setCate_id(tsNtCategoryIndices.get(i).getCate_id());
                tsNtCategoryIndexNew.setCate_img(tsNtCategoryIndices.get(i).getCate_img());
                tsNtCategoryIndexNew.setCate_name(tsNtCategoryIndices.get(i).getCate_name());
                tsNtCategoryIndexNews.add(i,tsNtCategoryIndexNew);
            }
            map.put("category", tsNtCategoryIndexNews);
            System.out.println("fdafafa");
            /**
             * 查询用户是否购买过一对一课程或者体验课
             */

            Integer hasReserved = 0;  //0：未买过课程  1：买过体验课，未买过一对一的课程    2：买过一对一的课程
            if(memb_id != null && memb_id != 0){
                 //买过体验课或者一对一的课程
                List<TdNtSetMealIsTrial> tdNtSetMealIsTrial=tdNtMembSetmealDao.selectSetmealIsTrial(memb_id);
                Integer count=tdNtSetMealIsTrial.size();
                 //买过一对一的课程
                if(count == 1 ){
                    //判断这节课是不是体验课
                    if(tdNtSetMealIsTrial.get(0).getIs_trial() == 1){
                        hasReserved = 1;
                    }else{
                        hasReserved = 2;
                    }
                }
                if(count >= 2) {
                    hasReserved = 2;
                }
            }
            map.put("hasReserved", hasReserved);
            //v4.3.1 去掉直播模块所有功能
//            /**
//             * 直播课程列表(id,状态,封面,标题,老师名称,报名人数)
//             */
//
//            List<TdNtLiveCourseHisNewList> tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListUnList(page);//直播中
//            if (tdNtLiveCourseHisLists == null){
//                tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListList(page);//待直播
//            }
//            else if (tdNtLiveCourseHisLists != null && tdNtLiveCourseHisLists.size() == 1) {
//                page.setPageSize(1);
//                tdNtLiveCourseHisLists.addAll(tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListList(page));
//            }
//            List<TdNtLiveCourseHisIndexFour> tdNtLiveCourseHisIndexFours = new ArrayList<>();
//            for (int i = 0 ; i < tdNtLiveCourseHisLists.size() ; i ++ ){
//                TdNtLiveCourseHisIndexFour tdNtLiveCourseHisIndexFour =  new TdNtLiveCourseHisIndexFour();
//                tdNtLiveCourseHisIndexFour.setLive_cou_his_id(tdNtLiveCourseHisLists.get(i).getLive_cou_his_id());
//                tdNtLiveCourseHisIndexFour.setLive_cou_his_name(tdNtLiveCourseHisLists.get(i).getLive_cou_his_name());
//                tdNtLiveCourseHisIndexFour.setLive_cou_his_picture(tdNtLiveCourseHisLists.get(i).getLive_cou_his_picture());
//                tdNtLiveCourseHisIndexFour.setLive_cou_his_time(tdNtLiveCourseHisLists.get(i).getLive_cou_his_time());
//                tdNtLiveCourseHisIndexFour.setLive_cou_size(tdNtLiveCourseHisLists.get(i).getLive_cou_size());
//                tdNtLiveCourseHisIndexFour.setLive_cou_state(tdNtLiveCourseHisLists.get(i).getLive_cou_state());
//                tdNtLiveCourseHisIndexFour.setTea_name(tdNtLiveCourseHisLists.get(i).getTea_name());
//                tdNtLiveCourseHisIndexFours.add(i,tdNtLiveCourseHisIndexFour);
//            }
//            map.put("lives", tdNtLiveCourseHisIndexFours);
            /**
             * 推荐老师列表(头像,状态,id,名称,学校,关注人数,教学时长,系统标签,类别标签)
             */
            Page page = new Page();
            page.setCurrentPage(0);
            page.setPageSize(10);
            page.setObj(0);
            List<TdNtTeacherList> tdNtTeacherLists = tdNtTeacherDao.findTdNtTeacherList(page);
            for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                if (tdNtTeacherLists.get(i).getDict_id() != null) {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                } else {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                }
                String country = tdNtTeacherLists.get(i).getNationality();
                if (country.equals("0") || country.equals("315")) {
                    country = "1";
                }
                String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
                tdNtTeacherLists.get(i).setNationality(nationality);
                /**
                 * 查找老师粉丝数
                 */
                int funsnumber = tdNtTeacherDao.findFunsSize(tdNtTeacherLists.get(i).getTea_id());
                tdNtTeacherLists.get(i).setFans_size(funsnumber);
//                /**
//                 * 查找是否收藏老师 1未收藏0已收藏
//                 */
                String collection_flag = "1";
                tdNtTeacherLists.get(i).setCollection_flag(collection_flag);
                /**
                 * 查找老师系统标签
                 */
                Map<String,Object> params = new HashMap<>();
                params.put("tea_id", tdNtTeacherLists.get(i).getTea_id());
                params.put("lab_type", 1);
                tdNtTeacherLists.get(i).setLabels(tdNtTeacherLabelDao.findTdNtTeacherListTeaIdAndType(params));
            }
            map.put("teachers", tdNtTeacherLists);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取首页数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    public RetInfo appointmentUpcoming(TdNtMember tdNtMember) {
        RetInfo retInfo = RetInfo.getSuccessInfo();

        try {
            UpcomingResponse upcomingResponse = tdNtReserveDao.findUpcomingByMembId(tdNtMember.getMemb_id());
            if (upcomingResponse == null) {
                retInfo.setTip("无预约课");
            }
            retInfo.setObj(upcomingResponse);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            retInfo = RetInfo.getServerErrorInfo(Lang.ZH);
        }
        return retInfo;
    }

    public RetInfo teacherRecommend() {
        RetInfo retInfo = RetInfo.getSuccessInfo();
        List<TeacherRecommendResponse> list = tdNtTeacherDao.findHotTeachers();
        retInfo.setObj(list);
        return retInfo;
    }

}
